<template>
  <div class="batch_list" v-if="showStatus">
    <p class="middle_line"></p>
    <el-tabs v-model="tabsValue" type="border-card" @tab-click="tabsChange" class="mes_tabs">
      <el-badge class="mark" :value="abNormalNum" v-if="abNormalNum"/>
      <el-badge class="mark third" :value="feedList" v-if="feedList"/>
      <el-tab-pane label="周转批次任务" :num="abNormalNum" name="first">
        <!--<span slot="label"><i class="el-icon-date"></i> 周转批次任务</span>-->
        <normal-batch-list :ids="workorderIds" ref="normalBatchList" @cancelBatch="cancelBatch"
                           @selectBatch="selectBatch"></normal-batch-list>
      </el-tab-pane>
      <el-tab-pane label="待处理异常任务" name="second">
        <abnormal-batch-list :ids="workorderIds" @abNormalList="getAbNormalList"
                             @abnormalSelectBatch="abnormalSelectBatch" ref="abnormalBatchList"></abnormal-batch-list>
      </el-tab-pane>
      <el-tab-pane label="待审核生产补料" name="third">
        <production-feed :ids="workorderIds" @getFeedList="getFeedList" @selectFeedList="selectFeedList"
                         ref="productionFeed"></production-feed>
      </el-tab-pane>
    </el-tabs>
    <div class="mes_btn table_btns" v-show="tabsValue === 'first'">
      <disabled-btn
        v-btn-permission="'revoke1'"
        :judge="revokeBtnStatus"
        iconClass="iconfont icon-btn_return"
        btnText="撤销"
        @judgeClick="revokeBatch"></disabled-btn>
      <disabled-btn
        v-btn-permission="'workstageSplit1'"
        :judge="splitBtnStatus"
        iconClass="iconfont icon-piliangchaifen"
        btnText="工序级拆分"
        @judgeClick="workstageSplit"></disabled-btn>
      <disabled-btn
        v-btn-permission="'pause1'"
        :judge="pauseBtnStatus"
        iconClass="iconfont icon-zanting"
        btnText="暂停"
        @judgeClick="changeBatch('Pause', '暂停')"></disabled-btn>
      <disabled-btn
        v-btn-permission="'resume1'"
        :judge="resumeBtnStatus"
        iconClass="iconfont icon-btn_recovery"
        btnText="恢复"
        @judgeClick="changeBatch('Resume', '恢复')"></disabled-btn>
      <disabled-btn
        v-btn-permission="'partTerminate1'"
        :judge="partTerminateBtnStatus"
        iconClass="iconfont icon-btn_frozen"
        btnText="分级终止"
        @judgeClick="changeBatch('PartTerminate', '分级终止')"></disabled-btn>
      <disabled-btn
        v-btn-permission="'terminate1'"
        :judge="terminateBtnStatus"
        iconClass="iconfont icon-btn_abend"
        btnText="终止"
        @judgeClick="changeBatch('Terminate', '终止')"></disabled-btn>
      <disabled-btn
        v-btn-permission="'batchSchedule1'"
        :judge="batchScheduleStatus"
        iconClass="iconfont icon-piliang"
        btnText="批量调度"
        btnType="primary"
        @judgeClick="batchSchedule()"></disabled-btn>
      <disabled-btn
        v-btn-permission="'normalSend1'"
        :judge="normalSendBtnStatus"
        iconClass="iconfont icon-btn_issued"
        btnText="下发"
        btnType="success"
        @judgeClick="sendBatch('Issue', 1)"></disabled-btn>
    </div>
    <div class="mes_btn table_btns" v-show="tabsValue === 'second'">
      <disabled-btn
        v-btn-permission="'abnormalSend2'"
        :judge="abnormalSendBtnStatus"
        iconClass="iconfont icon-btn_issued"
        btnText="下发"
        btnType="success"
        @judgeClick="sendBatch('Issue', 2)"></disabled-btn>
    </div>
    <div class="mes_btn table_btns" v-show="tabsValue === 'third'">
      <disabled-btn
        v-btn-permission="'revoke3'"
        :judge="revokeJudge"
        iconClass="iconfont icon-btn_return"
        btnText="退回"
        @judgeClick="feedHandle('Refused')"></disabled-btn>
      <disabled-btn
        v-btn-permission="'pass3'"
        :judge="revokeJudge"
        iconClass="iconfont icon-btn_Save"
        btnText="通过"
        btnType="success"
        @judgeClick="feedHandle('Request')"></disabled-btn>
    </div>
  </div>
</template>

<script>
import NormalBatchList from './NormalBatchList'
import AbnormalBatchList from './AbnormalBatchList'
import ProductionFeed from './ProductionFeed'

export default {
  name: 'BatchList',
  components: {
    NormalBatchList,
    AbnormalBatchList,
    ProductionFeed
  },
  data() {
    return {
      showStatus: true, // 组件是否显示
      tabsValue: 'first', // tab切换
      abNormalNum: 0, // 非正常任务数量
      workorderIds: '', // 工单ID
      selectData: [], // 全量批次勾选的列表
      abnormalSelectData: [], // 异常批次勾选的列表
      taskNumber: '', // 任务号
      feedList: 0, // 补料列表count
      selectFeedListData: [], // 补料列表所选数组
    }
  },
  computed: {
    // 验证是否已选
    _selected() {
      if (!this.selectData.length) return {msg: '请先选择周转批次！'}
    },
    _abselected() {
      if (!this.abnormalSelectData.length) return {msg: '请选择一条周转批次！'}
    },
    _feedSelected() {
      if (!this.selectFeedListData.length) return {msg: '请选择一条周转批次！'}
    },
    // 撤销批次
    revokeBtnStatus() {
      if (this._selected) return this._selected
      let status = false
      const msg = '请选择周转状态为"正常"或"已暂停" , 周转进度为"待下发"或"待派工" , 任务类型为"正常"的周转批次进行撤销'
      if (this.selectData.some((item) => item.turnoverStatusValue !== 'Normal' && item.turnoverStatusValue !== 'Paused')) {
        status = true
      }
      if (this.selectData.some((item) => item.turnoverProcessStatusValue !== 'ToBeIssued' && item.turnoverProcessStatusValue !== 'ToBeAssigned')) {
        status = true
      }
      if (this.selectData.some((item) => item.taskTypeValue !== 'Normal')) {
        status = true
      }
      return status ? {disabled: status, msg: msg} : {msg: false}
    },
    // 工序级拆分
    splitBtnStatus() {
      if (this._selected) return this._selected
      let status = false
      const msg = '请选择一条周转状态非"已终止"和"问题处理中" , 周转进度非"已完工" , 追溯要求为"批量追溯" , 计划数量大于1的周转批次进行拆分'
      if (this.selectData.length > 1) {
        status = true
      }
      if (this.selectData.some((item) => item.turnoverStatusValue === 'Terminated')) {
        status = true
      }
      if (this.selectData.some((item) => item.turnoverStatusValue === 'InProblem')) {
        status = true
      }
      if (this.selectData.some((item) => item.turnoverProcessStatusValue === 'Finished')) {
        status = true
      }
      if (this.selectData.some((item) => item.trackDemandValue === 'Single')) {
        status = true
      }
      if (this.selectData.length && !this.selectData.some((item) => item.productionQuantity > 1)) {
        status = true
      }
      return status ? {disabled: status, msg: msg} : {msg: false}
    },
    // 暂停批次
    pauseBtnStatus() {
      if (this._selected) return this._selected
      let status = false
      const msg = '请选择周转状态为"正常" , 周转进度非"已完工"的周转批次进行暂停'
      if (this.selectData.some((item) => item.turnoverStatusValue !== 'Normal')) {
        status = true
      }
      if (this.selectData.some((item) => item.turnoverProcessStatusValue === 'Finished')) {
        status = true
      }
      return status ? {disabled: status, msg: msg} : {msg: false}
    },
    // 恢复批次
    resumeBtnStatus() {
      if (this._selected) return this._selected
      let status = false
      const msg = '请选择周转状态为"已暂停"或"分级终止" , 周转进度非"已完工"的周转批次进行恢复'
      if (this.selectData.some((item) => item.turnoverStatusValue !== 'Paused' && item.turnoverStatusValue !== 'PartTerminated')) {
        status = true
      }
      if (this.selectData.some((item) => item.turnoverProcessStatusValue === 'Finished')) {
        status = true
      }
      return status ? {disabled: status, msg: msg} : {msg: false}
    },
    // 分级终止
    partTerminateBtnStatus() {
      if (this._selected) return this._selected
      let status = false
      const msg = '请选择周转状态为"正常" , 周转进度为"生产中" , 任务类型为"正常"的周转批次进行分级终止'
      if (this.selectData.some((item) => item.turnoverStatusValue !== 'Normal')) {
        status = true
      }
      if (this.selectData.some((item) => item.turnoverProcessStatusValue !== 'Producing')) {
        status = true
      }
      if (this.selectData.some((item) => item.taskTypeValue === 'Rework')) {
        status = true
      }
      return status ? {disabled: status, msg: msg} : {msg: false}
    },
    // 终止
    terminateBtnStatus() {
      if (this._selected) return this._selected
      let status = false
      const msg = '请选择周转状态非"问题处理中"和"已终止" , 周转进度非"已完成"的周转批次进行终止'
      if (this.selectData.some((item) => item.turnoverStatusValue === 'Terminated')) {
        status = true
      }
      if (this.selectData.some((item) => item.turnoverStatusValue === 'InProblem')) {
        status = true
      }
      if (this.selectData.some((item) => item.turnoverProcessStatusValue === 'Finished')) {
        status = true
      }
      return status ? {disabled: status, msg: msg} : {msg: false}
    },
    // 正常批次下发
    normalSendBtnStatus() {
      if (this._selected) return this._selected
      let status = false
      const msg = '请选择周转状态为"正常" , 周转进度为"待下发" , 任务类型均为"正常"或"返工返修"且属同一工单号的周转批次进行下发'
      if (this.selectData.some((item) => item.turnoverStatusValue !== 'Normal')) {
        status = true
      }
      if (this.selectData.some((item) => item.turnoverProcessStatusValue !== 'ToBeIssued')) {
        status = true
      }
      if (this.selectData.some((item) => item.workorderId !== this.selectData[0].workorderId)) {
        status = true
      }
      if (this.selectData.some((item) => item.taskTypeValue === 'Rework') && this.selectData.some((item) => item.taskTypeValue === 'Normal')) {
        status = true
      }
      return status ? {disabled: status, msg: msg} : {msg: false}
    },
    // 异常批次下发
    abnormalSendBtnStatus() {
      if (this._abselected) return this._abselected
      let status = false
      const msg = '请选择周转状态为"正常" , 周转进度为"待下发" ,且均属同一工单号的周转批次进行下发'
      if (this.abnormalSelectData.some((item) => item.turnoverStatusValue !== 'Normal')) {
        status = true
      }
      if (this.abnormalSelectData.some((item) => item.turnoverProcessStatusValue !== 'ToBeIssued')) {
        status = true
      }
      if (this.abnormalSelectData.some((item) => item.workorderId !== this.abnormalSelectData[0].workorderId)) {
        status = true
      }
      return status ? {disabled: status, msg: msg} : {msg: false}
    },
    // 修改判断
    revokeJudge() {
      if (this._feedSelected) return this._feedSelected
      /*if (this.selectFeedListData.some(item => item.materialRequestStatusValue !== 'Request' && item.materialRequestStatusValue !== 'ToBeApproved')) {
        return {
          msg: '只能撤销“要料中”或“待审核”状态的要料清单',
          disabled: true
        }
      }*/
      return {msg: false}
    },
    // 批量调度
    batchScheduleStatus() {
      if (this._selected) return this._selected
      let status = false
      const msg = '请选择周转进度为"待派工"或"待下发", 且属同一工单的周转批次进行批量调度'
      const filterList = this.selectData.filter((item) => item.turnoverProcessStatusValue === 'ToBeAssigned' || item.turnoverProcessStatusValue === 'ToBeIssued')
      if (this.selectData.length === 0) {
        status = true
      }
      if (this.selectData.length && this.selectData.length > filterList.length) {
        status = true
      }
      if (this.selectData.some((item) => item.workorderId !== this.selectData[0].workorderId)) {
        status = true
      }
      return status ? {disabled: status, msg: msg} : {msg: false}
    }
  },
  methods: {
    /**
     * 刷新周转批次列表
     * @param taskNumber 任务号
     */
    refresh(taskNumber) {
      this.taskNumber = taskNumber
      this.$refs.normalBatchList.refresh('', true, taskNumber)
      this.$refs.abnormalBatchList.refresh('', true, taskNumber)
      this.$refs.productionFeed.refresh('', true, taskNumber)
      this.workorderIds = ''
    },
    // 全量选择周转批次
    selectBatch(selectData) {
      this.selectData = selectData
    },
    // 异常批次选择
    abnormalSelectBatch(selectData) {
      this.abnormalSelectData = selectData
    },
    // 补料选择
    selectFeedList(selectData) {
      this.selectFeedListData = selectData
    },
    /**
     * 获取记录id并请求已拆分的列表
     * @param ids 工单id
     * @param taskNumber 任务号
     */
    showBatchList(ids, taskNumber) {
      this.showStatus = true
      this.workorderIds = ids
      this.taskNumber = taskNumber
      this.tabsValue = 'first'
      setTimeout(() => {
        this.$refs.normalBatchList.refresh(ids, true, taskNumber)
        this.$refs.abnormalBatchList.refresh(ids, true, taskNumber)
        this.$refs.productionFeed.refresh(ids, true, taskNumber)
      })
    },
    // tab切换
    tabsChange() {
      if (this.tabsValue === 'first') {
        this.selectData = []
        this.$refs.normalBatchList.refresh(this.workorderIds, false, this.taskNumber)
      } else if (this.tabsValue === 'second') {
        this.abnormalSelectData = []
        this.$refs.abnormalBatchList.refresh(this.workorderIds, false, this.taskNumber)
      } else {
        this.selectFeedListData = []
        this.$refs.productionFeed.refresh(this.workorderIds, true, this.taskNumber)
      }
    },
    // 获取异常任务列表总数
    getAbNormalList(val) {
      this.abNormalNum = val
    },
    // 获取补料数量
    getFeedList(val) {
      this.feedList = val
    },
    // 撤销任务
    cancelBatch() {
      this.$emit('cancelBatch')
    },
    // 按钮事件
    // 撤销
    revokeBatch() {
      this.$refs.normalBatchList.revokeBatch()
    },
    // 工序级拆分
    workstageSplit() {
      this.$refs.normalBatchList.workstageSplit()
    },
    // 暂停、恢复、分级终止、终止
    changeBatch(status, str) {
      this.$refs.normalBatchList.changeBatch(status, str)
    },
    /**
     * 周转批次下发
     * @param type 批次类型: 1：正常类型批次；2：异常批次
     */
    sendBatch(status, type) {
      switch (type) {
        case 1:
          this.$refs.normalBatchList.sendBatch(status)
          break
        case 2:
          this.$refs.abnormalBatchList.sendBatch(status)
          break
      }
    },
    // 补料退回/通过
    feedHandle(value) {
      this.$refs.productionFeed.applyHandle(value)
    },
    // 批量调度
    batchSchedule() {
      this.$refs.normalBatchList.batchSchedule()
    }
  }
}
</script>

<style lang="less">
  .batch_list {
    position: relative;
    .middle_line {
      border-top: 1px solid #eee;
      margin-bottom: 20px;
    }
    .table_btns {
      position: absolute;
      right: 0;
      top: 32px;
    }
    .el-tab-pane {
      height: 440px;
    }
    .el-tabs__content {
      overflow: visible !important;
      .el-badge {
        position: absolute;
        top: -47px;
        z-index: 99;
        left: 250px;
        &.third {
          left: 385px;
        }
      }
    }
    .table_container {
      padding: 0 !important;
    }
    .search_ordinary {
      margin-left: 0 !important;
    }
    .pagination {
      padding-left: 0 !important;
      .el-pagination {
        padding-left: 0 !important;
        .btn-prev {
          margin-left: 0 !important;
        }
      }
    }
  }
</style>
